package com.example.es;

import com.example.util.EsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.*;

@SpringBootTest
@Slf4j
public class index {

    @Test
    public void searchIndex() throws IOException {
        // 创建 es 客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("172.16.10.234", 9200, "http"))
        );
        String auditlog = "audit_log_2023083*";
        GetIndexRequest getIndexRequest = new GetIndexRequest(auditlog);
        try {
            GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);
            String[] indices = getIndexResponse.getIndices();
            Arrays.stream(indices).forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }

    @Test
    public void searchIndex2() throws IOException {
        // 创建 es 客户端
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("172.16.10.234", 9200, "http"))
        );
        String index = "audit_log_";
        Date  startDate= new Date();
        Date  endDate= new Date();
        Map<String,Object> map =  new HashMap<>();
        map.put("startTime",startDate.getTime()-7*24*60*60*1000);
        map.put("endTime",endDate.getTime());
        List<String> stringList = EsUtil.getIndexes(map,index,client);
        String[] ddd = stringList.toArray(new String[0]);
        for (String str:ddd) {
            System.out.println(str);
        }
    }

}
